package com.band.controller;

import com.band.AliyunProperties;
import com.band.entity.Result;
import com.band.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunProperties aliyunProperties;
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = AliyunOSSUtils.upload(aliyunProperties.getEndpoint(), aliyunProperties.getBucket(), file.getBytes(), extName);
        log.info("上传文件：{}", originalFilename);

        return Result.success(url);
    }
}
